package as.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import as.entity.Recycler;
import as.entity.Register;
import as.entity.Student;

public class RecyclerDAO extends HibernateDaoSupport{
	
	public void saveRecycler(Recycler recycler){
		this.getHibernateTemplate().save(recycler);
	}
	
	public void removeRecycler(Recycler recycler){
		this.getHibernateTemplate().delete(recycler);
	}
	
	public boolean isRecyclerHas(){

		final String hql = "from Recycler order by registerNo asc";
		List<Recycler> list = new ArrayList<Recycler>();

		list = this.getHibernateTemplate().find(hql);
		
		if(list.size()==0){
			return false;
		}
		else{      
			return true;
		}

	}
	
	public int findRecyclerFirst(){
		final String hql = "from Recycler order by register_no asc";
		List<Recycler> list = new ArrayList<Recycler>();
		list = this.getHibernateTemplate().find(hql);
		return list.get(0).getRegisterNo();
	}
	
	public Recycler findRecycler(int id){

		Recycler recycler = (Recycler) this.getHibernateTemplate().get(Recycler.class, id);
		return recycler;

	}
	
	public int[] findAllRemoveRegistNo(long id){
		final String hql = "from Register  where course_id=? ";
		List<Register> list = new ArrayList<Register>();
		list=this.getHibernateTemplate().find(hql,id);
		int size=list.size();
		int[]registerNos =new int[size];
		for(int i=0;i<size;i++){
			registerNos[i]=list.get(i).getRegisterNo();
		}
//		Iterator<Register> iterator = list.iterator();
//		List<Student> list2 = new ArrayList<Student>();
//		while(iterator.hasNext()){
//			Register register = iterator.next();
//			Student student = register.getStudent();
//			student = (Student) this.getHibernateTemplate().get(Student.class, student.getStudentId());
//			list2.add(student);
//		}
		return registerNos;
	}
	
}
